<?php
header("Content-type: text/html; charset=utf-8");
ini_set('date.timezone','Asia/Shanghai');
$data = DeleteHtml2(curlGet("http://www.bwlc.net/bulletin/prevtrax.html"));
preg_match('/<tr class=\"\"><td>(.*?)<\/td><td>(.*?)<\/td><td>(.*?)<\/td><\/tr>/',$data,$m);

// $dsn='mysql:host=127.0.0.1; dbname=pk;';
// $dbh=new PDO($dsn,'root','');
// $stmt=$dbh->query('SELECT id,periodnumber FROM think_number where periodnumber =637736');
// if($stmt!=false){
// 	$row=$stmt->fetch();}
// 	else{
// 	$row=0;
// 	}

try{  
    $pdo=new PDO("mysql:host=127.0.0.1;dbname=pk","root","");  
}catch (PDOException $e){  
    die("fail to connect mysql".$e->getMessage());  
}


 //    $cache = new Cache(60,'Runtime/Cache/');
	// //从缓存从读取键值 $key 的数据 
	// $values = $cache->get('pk10data'); 
	// //如果没有缓存数据 
	// if ($values == false) { 
	// //写入键值 $key 的数据 
	// $cache->put('pk10data', $m[1]); 
	// } else { 
	// //insert code here... 
		
	// } 

			if(CF('pk10data')!=$m[1]){
				$sql='SELECT id,periodnumber FROM think_number where periodnumber ={$m[1]}';
				$res=$pdo->query($sql);
					if(!$res){
						$map['awardnumbers'] = $m[2];//开奖号码
						$map['awardtime'] = $m[3];//开奖公告时间
						$map['time'] = strtotime($m[3]);//要存储时间
						$map['periodnumber'] = $m[1];//期号
						$info = explode(',', $map['awardnumbers']);
						for($i=0;$i<count($info);$i++){
							if($info[$i]<10){
								$info[$i] = substr($info[$i], 1);
							}
						}
						$map['number'] = serialize($info);
						if($info[0]>$info[9]){
							$lh[0] = '龙';
						}else{
							$lh[0] = '虎';
						}
						if($info[1]>$info[8]){
							$lh[1] = '龙';
						}else{
							$lh[1] = '虎';
						}
						if($info[2]>$info[7]){
							$lh[2] = '龙';
						}else{
							$lh[2] = '虎';
						}
						if($info[3]>$info[6]){
							$lh[3] = '龙';
						}else{
							$lh[3] = '虎';
						}
						if($info[4]>$info[5]){
							$lh[4] = '龙';
						}else{
							$lh[4] = '虎';
						}
						$map['lh'] = serialize($lh);
						$map['tema'] = $info[0]+$info[1];
						if($map['tema'] % 2 == 0){
							$map['tema_ds'] = '双';
						}else{
							$map['tema_ds'] = '单';
						}
						if($map['tema']>=12){
							$map['tema_dx'] = '大';
						}else{
							$map['tema_dx'] = '小';
						}
						if($map['tema']>=3 && $map['tema']<=7){
							$map['tema_dw'] = 'A';
						}
						if($map['tema']>=8 && $map['tema']<=14){
							$map['tema_dw'] = 'B';
						}
						if($map['tema']>=15 && $map['tema']<=19){
							$map['tema_dw'] = 'C';
						}
						if($info[0]>$info[1]){
							$map['zx'] = '庄';
						}else{
							$map['zx'] = '闲';
						}
						$sql2='INSERT INTO think_number VALUES ({$map['periodnumber']},'."$map['awardtime']".','."$map['awardnumbers']".','."$map['lh']".','."$map['tema']".','."$map['tema_dx']".','."$map['tema_ds']".','."$map['zx']".','."$map['tema_dw']".','."$map['number']".','."$map['time']".')';
						var_dump($sql2);
						exit();
						$res1 =$pdo->exec($sql2);
						if($res1){
						CF('pk10data',$m[1]);
						}
						

					}
				}

				ignore_user_abort();

				/*get请求获取数据*/
function curlGet($url){
	$ch = curl_init();
	//设置选项，包括URL
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	//执行并获取HTML文档内容
	$output = curl_exec($ch);
	//释放curl句柄
	curl_close($ch);
	return $output;
}

function DeleteHtml2($str)
{
$str = trim($str);
$str=preg_replace("{\t}","",$str);
$str=preg_replace("{\r\n}","",$str);
$str=preg_replace("{\r}","",$str);
$str=preg_replace("{\n}","",$str);
return $str;
}

/**
 * 快速文件数据读取和保存 针对简单类型数据 字符串、数组
 * @param string $name 缓存名称
 * @param mixed $value 缓存值
 * @param string $path 缓存路径
 * @return mixed
 */



function CF($key,$value='')
{
	$cache = new Cache(60,'Runtime/Cache/');
	//从缓存从读取键值 $key 的数据 
	$values = $cache->get($key); 
	//如果没有缓存数据 
	if ($values == false) { 
	//写入键值 $key 的数据 
	$cache->put($key, $value); 
	} else { 
	//insert code here... 
		
	} 
	return $values;
}

class Cache {
	private $cache_path;//path for the cache
	private $cache_expire;//seconds that the cache expires

	//cache constructor, optional expiring time and cache path
	public function Cache($exp_time=3600,$path="cache/"){
		$this->cache_expire=$exp_time;
		$this->cache_path=$path;
	}

	//returns the filename for the cache
	private function fileName($key){
		return $this->cache_path.md5($key);
	}

	//creates new cache files with the given data, $key== name of the cache, data the info/values to store
	public function put($key, $data){
		$values = serialize($data);
		$filename = $this->fileName($key);
		$file = fopen($filename, 'w');
	    if ($file){//able to create the file
	        fwrite($file, $values);
	        fclose($file);
	    }
	    else return false;
	}

	//returns cache for the given key
	public function get($key){
		$filename = $this->fileName($key);
		if (!file_exists($filename) || !is_readable($filename)){//can't read the cache
			return false;
		}
		if ( time() < (filemtime($filename) + $this->cache_expire) ) {//cache for the key not expired
			$file = fopen($filename, "r");// read data file
	        if ($file){//able to open the file
	            $data = fread($file, filesize($filename));
	            fclose($file);
	            return unserialize($data);//return the values
	        }
	        else return false;
		}
		else return false;//was expired you need to create new
 	}
}